<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

    <div id="app">
      <div v-jspang="color" id="demo">{{num}}</div>
      <div>
        <button @click="add">Add</button>
        <button onclick="unbind()">unbind</button>
      </div>
    </div>

    <script>
      // 解绑
      function unbind() {
        app.$destroy();
      }

      Vue.directive("jspang", {
        bind: function (el, binding, vnode) {
          //被绑定
          console.log("1 - bind");
          el.style = "color:" + binding.value;
        },
        inserted: function () {
          //绑定到节点
          console.log("2 - inserted");
        },
        update: function () {
          //组件更新
          console.log("3 - update");
        },
        componentUpdated: function () {
          //组件更新完成
          console.log("4 - componentUpdated");
        },
        unbind: function () {
          //解绑
          console.log("5 - bind");
        },
      });

      var app = new Vue({
        el: "#app",
        data: {
          num: 10,
          color: "green",
        },
        methods: {
          add: function () {
            this.num++;
          },
        },
      });
    </script>
  </body>
</html>
